//
//  CLspecialViewController.m
//  I-want-go
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLspecialViewController.h"
#import "CLtool.h"
#import "BaseModel.h"
#import "CLspecialModel.h"
#import "CLspecialCollectionCell.h"
#import "CLpresntView.h"
#import "CLeverydayController.h"
#import "CLscreen.h"
#import "CLSingleton.h"
@interface CLspecialViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)MBProgressHUD *hub;

@end

@implementation CLspecialViewController
- (void)dealloc {
    [_arr release];
    [_collectionView release];
    [_hub release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self iswlan];
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hub.labelText = @"正在加载";
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"专题";
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;

    NSLog(@"%d", [CLSingleton shareSingleton].strState);
    
    NSLog(@"%f", SHEIGHT);
     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(265 * SWIDTH, 460 * SHEIGHT);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(50 * SWIDTH, 50 *SHEIGHT, 275 * SWIDTH, 460 * SHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[CLspecialCollectionCell class] forCellWithReuseIdentifier:@"raue"];
    [self.collectionView registerClass:[CLspecialCollectionCell class] forCellWithReuseIdentifier:@"reee"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
//    // 隐藏navigationbar
//    self.navigationController.navigationBarHidden = YES;
   
    
    
    [self createDate];
}

// 状态
- (void)iswlan {
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == 1) {
            [CLSingleton shareSingleton].strState = YES;
            self.tabBarController.tabBar.hidden = NO;
                UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"已连接" message:@"为2g/3g网" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:aleryController animated:YES completion:nil];
                UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            
                [aleryController addAction:leftAction];
            [self createDate];

         }
        else if (status == 2) {
            [CLSingleton shareSingleton].strState = YES;
            self.tabBarController.tabBar.hidden = NO;
            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"已连接" message:@"为wifi连接" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aleryController animated:YES completion:nil];
            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [aleryController addAction:leftAction];
            [self createDate];

//            [self.collectionView reloadData];
        }
        else if (status == 0) {
            [CLSingleton shareSingleton].strState = NO;
            self.tabBarController.tabBar.hidden = YES;
            UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"无连接" message:@"亲,连网再看" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:aleryController animated:YES completion:nil];
            UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [aleryController addAction:leftAction];
        }
    }];
}

- (void)createDate {
    
  
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://open3.bantangapp.com/recommend/index?app_installtime=1445332836.175734&app_versions=4.2.2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&page=0&pagesize=20&screensize=640&track_device_info=iPhone4%2C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&v=7" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arr = [NSMutableArray array];
        NSDictionary *dic = responseObject;
        self.arr = [CLspecialModel baseModelByArr:dic[@"data"][@"entry_list"]];
        self.hub.hidden = YES;
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        self.hub.hidden = YES;
        }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([CLSingleton shareSingleton].strState == NO && self.arr.count == 0) {
        return 1;
    } else if ([CLSingleton shareSingleton].strState == YES){
    return self.arr.count - 2;
    } else {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([CLSingleton shareSingleton].strState == NO) {
        CLspecialCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reee" forIndexPath:indexPath];
        cell.myImageView.backgroundColor = [UIColor yellowColor];
////        CLspecialModel *tempSpe = self.arr[indexPath.row];
//        
        cell.myImageView.image = [UIImage imageNamed:@"go3.png"];
        NSLog(@"1");
        return cell;
        
    } else {
    CLspecialCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"raue" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor yellowColor];
    CLspecialModel *tempSpe = self.arr[indexPath.row];
//    NSLog(@"%@", tempSpe.pic1);
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:tempSpe.pic1]];
    
    return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([CLSingleton shareSingleton].strState == NO) {
        UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"无连接" message:@"亲,连网再看" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aleryController animated:YES completion:nil];
        UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [aleryController addAction:leftAction];
    } else {
    
    if (indexPath.row == 0) {
        CLpresntView *presentVC = [[CLpresntView alloc] init];
        [self.navigationController pushViewController:presentVC animated:YES];
//        self.hidesBottomBarWhenPushed = YES;
     }
    else {
        CLeverydayController *shineVC = [[CLeverydayController alloc] init];
        [self.navigationController pushViewController:shineVC animated:YES];
        CLspecialModel *specialV = self.arr[indexPath.row];
        shineVC.strId = specialV.picid;
        shineVC.picStr = specialV.pic2;
        
    }
    }
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
